<?php

class Magestore_Mageconnector_Helper_Data extends Mage_Core_Helper_Abstract {

    /**
     * Get list store view code by store base url. Store base url is url of registed store
     *
     * @param string $baseUrl
     * @return string
     */
    public function getListStoreViewCodesByStoreBaseUrl($baseUrl) {
        $cWebsites = Mage::getResourceModel('core/website_collection');
        $xml = '<?xml version="1.0" encoding="utf-8"?><views>';
        $baseUrl = str_replace('www.', '', $baseUrl);
        $baseUrl = str_replace('http://', '', $baseUrl);
        $baseUrl = str_replace('https://', '', $baseUrl);

        $i = 0;
        foreach ($cWebsites as $website) {
            $cGroups = $website->getGroupCollection();
            foreach ($cGroups as $group) {
                $cStores = $group->getStoreCollection();
                foreach ($cStores as $store) {
                    if (!$store->getIsActive()) {
                        continue;
                    }
                    $storeBaseUrlConfig = Mage::app()->getStore($store->getId())->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
                    $storeBaseUrlConfig = str_replace('www.', '', $storeBaseUrlConfig);
                    $storeBaseUrlConfig = str_replace('http://', '', $storeBaseUrlConfig);
                    $storeBaseUrlConfig = str_replace('https://', '', $storeBaseUrlConfig);

                    if ($storeBaseUrlConfig != $baseUrl) {
                        continue;
                    }
                    $isDefault = false;
                    if ($website->getIsDefault() &&
                            $website->getDefaultGroupId() == $group->getId() &&
                            $group->getDefaultStoreId() == $store->getId()) {
                        $isDefault = true;
                    }

                    $xml .= '<view order="#' . (++$i) . '">'
                            . '<code><![CDATA[' . $store->getCode() . ']]></code>'
                            . '<name><![CDATA[' . $store->getName() . ']]></name>'
                            . '<is_active>' . $store->getIsActive() . '</is_active>'
                            . '<is_default>' . $isDefault . '</is_default>'
                            . '</view>';
                }
            }
        }

        $xml .= '</views>';
        return $xml;
    }

    private static $dStore = null;

    /**
     * Get Defaule Store By Store Base Url
     *
     * @param string $baseUrl
     * @return Mage_Core_Model_Store
     */
    public function getDefaultStoreByStoreBaseUrl($baseUrl) {
        if (is_null(self::$dStore)) {
            $cWebsites = Mage::getResourceModel('core/website_collection');
            $baseUrl = str_replace('www.', '', $baseUrl);
            $baseUrl = str_replace('http://', '', $baseUrl);
            $baseUrl = str_replace('https://', '', $baseUrl);

            foreach ($cWebsites as $website) {
                $cGroups = $website->getGroupCollection();
                foreach ($cGroups as $group) {
                    $cStores = $group->getStoreCollection();
                    foreach ($cStores as $store) {
                        if (!$store->getIsActive()) {
                            continue;
                        }
                        $storeBaseUrlConfig = Mage::app()->getStore($store->getId())->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
                        $storeBaseUrlConfig = str_replace('www.', '', $storeBaseUrlConfig);
                        $storeBaseUrlConfig = str_replace('http://', '', $storeBaseUrlConfig);
                        $storeBaseUrlConfig = str_replace('https://', '', $storeBaseUrlConfig);

                        if ($storeBaseUrlConfig != $baseUrl) {
                            continue;
                        }

                        if (is_null(self::$dStore)) {
                            self::$dStore = $store;
                        }
                        if ($website->getIsDefault() &&
                                $website->getDefaultGroupId() == $group->getId() &&
                                $group->getDefaultStoreId() == $store->getId()) {
                            self::$dStore = $store;
                            return $store;
                        }
                    }
                }
            }
        }
        if (is_null(self::$dStore)) {
            throw new Magestore_Mageconnector_Exception(Magestore_Mageconnector_Exception::BASE_URL_INVALID);
        } else {
            return self::$dStore;
        }
    }

}